package com.syqc.videos.real

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import autodispose2.autoDispose
import com.syqc.comlib.AppConfigs
import com.syqc.comlib.UserConstant
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.entity.VideoAlarm
import com.syqc.monitor.R
import com.syqc.net.ServiceHelper
import com.syqc.utils.Keys
import io.reactivex.rxjava3.core.Flowable
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.subjects.PublishSubject
import java.util.concurrent.TimeUnit

/**
 *@author  hfjs
 *@time   2024/12/31 9:53
 *@desc
 */
class VideoAlarmVM : AutoDisposeViewModel() {
    private val alarmsEntity = arrayListOf<VideoAlarm>()
    //刷新时间
    private var nowTime = getRefresh()
    private val timeSub by lazy { PublishSubject.create<Int>() }
    fun timeObserver() = timeSub.hide()

    //是否处于显示状态
    var isHidden = false
    var carId: String? = null
        set(value) {
            if (value != null && !isHidden) {
                stopTime()
                startTime(value)
            }
            field = value
        }

    private val _alarmLive = MutableLiveData<List<VideoAlarm>>()
    val alarmLive: LiveData<List<VideoAlarm>> = _alarmLive

    init {
        initAlarms()
    }

    /** 获取定位信息 */
    private fun getLocation(carId: String?) {
        if (carId.isNullOrEmpty()) {
            toast(R.string.hint_toast_choose_car)
            stopTime()
        } else
            ServiceHelper.getRepository().getGpsRInfo("", carId)
                .autoDispose(this)
                .subscribe({
                    if (it.isNullOrEmpty()) {
                        toast(R.string.toast_no_location_msg)
                        stopTime()
                    } else {
                        it[0].videoAlarmTypes?.let { item ->
                            alarmsEntity[0].isNormal = item.contains("vsm")
                            alarmsEntity[1].isNormal = item.contains("vsh")
                            alarmsEntity[2].isNormal = item.contains("vsb")
                            alarmsEntity[3].isNormal = item.contains("vtb")
                            alarmsEntity[4].isNormal = item.contains("vsv")
                        }
                        _alarmLive.postValue(alarmsEntity)
                    }
                }, error)
    }

    private var subscribe: Disposable? = null
    private fun getRefresh(): Int =
        if (UserConstant.loginType == Keys.LOGIN_TYPE_CAR) {
            5
        } else {
            AppConfigs.mapRefresh
        }


    /** 开始请求 */
    private fun startTime(carId: String) {
        subscribe?.dispose()
        nowTime = getRefresh()
        timeSub.onNext(nowTime)
        getLocation(carId)
        subscribe = Flowable.interval(0, 1, TimeUnit.SECONDS)
            .autoDispose(this)
            .subscribe {
                if (nowTime == 1) {
                    nowTime = getRefresh()
                } else {
                    nowTime--
                }
                timeSub.onNext(nowTime)
                if (nowTime == 1) {
                    getLocation(carId)
                }

            }
    }


    /** 停止请求 */
    fun stopTime() {
        subscribe?.dispose()
    }


    private fun initAlarms() {
        alarmsEntity.clear()
        alarmsEntity.add(VideoAlarm(R.mipmap.video_alarm_1, R.string.desc_video_alarm_1))
        alarmsEntity.add(VideoAlarm(R.mipmap.video_alarm_2, R.string.desc_video_alarm_2))
        alarmsEntity.add(VideoAlarm(R.mipmap.video_alarm_3, R.string.desc_video_alarm_3))
        alarmsEntity.add(VideoAlarm(R.mipmap.video_alarm_4, R.string.desc_video_alarm_4))
        alarmsEntity.add(VideoAlarm(R.mipmap.video_alarm_5, R.string.desc_video_alarm_5))
        _alarmLive.postValue(alarmsEntity)
    }
}